﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer.DAL;
using Common;

namespace DataAccessLayer.DAL
{
    public class AuctionTransactionDAL
    {
        public static void AddAuctionTransaction(AuctionTransaction auc)
        {
            using (var context = new ReverseAuctionEntities())
            {
                context.AuctionTransactions.AddObject(auc);
                context.SaveChanges();
            }
        }

        public static AuctionTransaction GetAuctionTransaction(int aucID)
        {
            using (var context = new ReverseAuctionEntities())
            {
                return context.AuctionTransactions.SingleOrDefault(u => u.AuctionID == aucID);
            }
        }

        public static List<AuctionTransaction> GetAuctionTransactionByUserID(int userID)
        {
            List<AuctionTransaction> list = new List<AuctionTransaction>();
            using (var context = new ReverseAuctionEntities())
            {
                var result = context.AuctionTransactions.Include("Auction").Where(u => u.Status == (int)AuctionTransactionStatus.Waitting);
                
                foreach (var item in result)
                {
                    if (item.Auction.CreateByUserID == userID)
                        list.Add(item);
                }
            }
            return list;
        }

        public static void BuyerPaymentAuctionTransaction(int auctionID ,int status)
        {
            using (var context = new ReverseAuctionEntities())
            {
                var result = context.Auctions.Include("AuctionTransaction").SingleOrDefault(u => u.ID == auctionID);
                if (result != null)
                {
                    result.Status = (int)AuctionStatus.Finish;
                    result.AuctionTransaction.Status = status;
                    context.SaveChanges();
                }
            }
        }
    }
}
